﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;

namespace ACPLogAnalyzer
{
    public enum GraphType
    {
        NoSelection,                // Place-holder
        FWHM,                       // All FWHM measurements for the current log
        FWHMAvgAllLogs,             // Average FWHM measurements for all logs
        HFD,                        // All HFD measurements for the current log
        HFDAvgAllLogs,              // Average HFD measurements for all logs
        FWHMvHFD,                   // Compare FWHM v HFD
        FWHMvHFDAllLogs,            // Compare FWHM v HFD for all logs
        PointErrorObj,              // All pointing errors (object slew) for the current log
        PointErrorObjAllLogs,       // Average pointing error (object slew) for all logs
        PointErrorCntr,             // All pointing errors (center slew) for the current log
        PointErrorCntrAllLogs,      // Average pointing errors (center slew) for all logs
        GuiderStartUpTime,          // All guider start-up times for current log
        GuiderStartUpTimeAllLogs,   // Average guider start-up times for all logs
        GuiderSettleTime,           // All guider settle times for current log
        GuiderSettleTimeLogAllLogs, // Average guider settle times for all logs
        FilterChangeTime,           // All filter change times for current log
        FilterChangeTimeAllLogs,    // Average filter change times for all logs
        PtExpSolveTime,             // All pointing exposure/plate solve times for the current log
        PtExpSolveTimeAllLogs,      // Average pointing exposure/plate solve times for all logs
        SlewTimeTgts,               // All slew times (targets) for the current log
        SlewTimeTgtsAllLogs,        // Average slew times (targets) for all logs
        AFTimes,                    // All auto-focus times for the current log
        AFTimesAllLogs              // Average auto-focus times for all logs
    }

    /// <summary>
    /// Interaction logic for Graph.xaml
    /// </summary>
    public partial class Graph : Window
    {
        private MainWindow m_mainWnd;  // The main app window
        public MainWindow MainWnd
        {
            get { return m_mainWnd; }
            set { m_mainWnd = value; }
        }

        private Log m_log;  // The log the data is derived from
        public Log Log
        {
            get { return m_log; }
            set { m_log = value; }
        }

        private List<KeyValuePair<object, object>> m_dataSourceSeries1;  // Set by caller to DataSource property 
        public List<KeyValuePair<object, object>> DataSourceSeries1  // string is the x-axis (normally time or date), object is the data to plot
        {
            get
            {
                return m_dataSourceSeries1;
            }
            set
            {
                m_dataSourceSeries1 = value;
                graphUC.DataSourceSeries1 = m_dataSourceSeries1;
            }
        }

        private List<KeyValuePair<object, object>> m_dataSourceSeries2;  // Set by caller to DataSourceSeries2 property 
        public List<KeyValuePair<object, object>> DataSourceSeries2  
        {
            get
            {
                return m_dataSourceSeries2;
            }
            set
            {
                m_dataSourceSeries2 = value;
                graphUC.DataSourceSeries2 = m_dataSourceSeries2;
            }
        }

        private List<KeyValuePair<object, object>> m_dataSourceSeries3;  // Set by caller to DataSourceSeries3 property 
        public List<KeyValuePair<object, object>> DataSourceSeries3
        {
            get
            {
                return m_dataSourceSeries3;
            }
            set
            {
                m_dataSourceSeries3 = value;
                graphUC.DataSourceSeries3 = m_dataSourceSeries3;
            }
        }

        public Graph() : this(null, null) { }
        public Graph(MainWindow wnd, Log log)
        {
            // The calling thread must be STA (the main thread) or an exception is thrown
            InitializeComponent();

            m_mainWnd = wnd;
            m_log = log;
            graphUC.DataPointSelectionChanged += new GraphUserControl.DataPointSelected(graphUC_DataPointSelectedEvent);
        }

        void graphUC_DataPointSelectedEvent(object sender, DataPointSelectedEventArgs e)
        {
            if (m_mainWnd != null)
            {
                // Scroll the main windows' log to match the datapoint selected in the graph
                m_mainWnd.ScrollLogToTime(e.Data, e.EventType, MapEventToLineNumber(e.Data, e.EventType));
            }
        }

        private int MapEventToLineNumber(KeyValuePair<object, object> data, LogEventType logEventType)
        {
            string tmpEventDateTimeStr = null;
            try
            {
                foreach (LogEvent le in m_log.LogEvents)
                {
                    if (le.EventType != logEventType)
                        continue;

                    if (le.StartDate.HasValue &&
                        le.Data != null &&
                        data.Value != null &&
                        ((double)le.Data) == ((double)data.Value))
                    {
                        tmpEventDateTimeStr = le.StartDate.Value.ToLongTimeString();
                        if(tmpEventDateTimeStr.CompareTo(data.Key.ToString()) == 0)
                            return le.LineNumber;
                    }
                }
            }
            catch
            {
            }

            return -1;
        }
    }
}
